﻿using BPMS.Utility.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace BPMS.Web.Extends.Attributes
{
    public class ValidateIPAddressAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext == null || filterContext.HttpContext == null || filterContext.HttpContext.Request == null)
            {
                return;
            }

            if (filterContext.IsChildAction)
            {
                return;
            }

            if (!"GET".Equals(filterContext.HttpContext.Request.HttpMethod, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }
            string cuttentIP = ClientUtility.GetClientIP();
            //if(!string.is)
            if (!string.IsNullOrWhiteSpace(cuttentIP))
            {
                //TODO 验证当前IP是否在黑名单中

                bool isAccessDenied = false;//是否拒绝访问
                if (isAccessDenied)
                {
                    filterContext.Result = new RedirectResult(URLUtility.RootUrl + "/error/accessdenied");
                }
            }
            return;
        }
    }
}
